package com.javafactroy.kunlong.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.javafactroy.kunlong.common.dto.XmlDto;
import com.javafactroy.kunlong.common.dto.XmlDtos;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

@Slf4j
public class XmlUtils {
    //private static Logger log = LoggerFactory.getLogger(XmlUtils.class);

    /**
     * 将bean转换成xml字符串
     *
     * @param bean
     * @return
     */
    public static String bean2Xml(Object bean) {
        // 创建输出流
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(bean.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(bean, sw);
        } catch (JAXBException e) {
            if (log.isDebugEnabled()) {
                log.debug("", e);
            }
            return null;
        }
        return sw.toString();
    }

    /**
     * 将xml解析成bean
     *
     * @param xmlStr
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T xml2Bean(String xmlStr, Class<T> clazz) {
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            Object xmlObject = unmarshaller.unmarshal(sr);
            return (T) xmlObject;
        } catch (JAXBException e) {
            if (log.isDebugEnabled()) {
                log.debug("", e);
            }
            return null;
        }
    }

    public static String object2Xml(Object o,String root){
        JSON json = JSONUtil.parse( o );
        String xml = JSONUtil.toXmlStr(json);
        return StrUtil.format("<{}>{}</{}>",root,xml,root);
    }

    static String xml = " <parent>\n" +
            "        <groupId>com.javafactroy.kunlong</groupId>\n" +
            "        <artifactId>service</artifactId>\n" +
            "        <version>2022</version>\n" +
            "    </parent>";
    static  String xmls="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<project>\n" +
            "    <parent>\n" +
            "        <groupId>com.javafactroy.kunlong</groupId>\n" +
            "        <artifactId>service</artifactId>\n" +
            "        <version>2022</version>\n" +
            "    </parent>\n" +
            "    <parent>\n" +
            "        <groupId>com.javafactroy.kunlong</groupId>\n" +
            "        <artifactId>service</artifactId>\n" +
            "        <version>2022</version>\n" +
            "    </parent>\n" +
            "</project>";

    public static void main(String[] args) {
        XmlDto xmlDto = XmlUtils.xml2Bean(xml, XmlDto.class);
        log.info("{}", xmlDto.toGsonStr());
        String sxmls = XmlUtils.bean2Xml(xmlDto);
        log.info("{}", sxmls);

        XmlDtos xmlDtos = XmlUtils.xml2Bean(xmls , XmlDtos.class);
        log.info("xmlDtos={}", JSONUtil.toJsonPrettyStr(xmlDtos));
        String sxmlss = XmlUtils.bean2Xml(xmlDtos);
        log.info("sxmlss={}", sxmlss);

    }
}
